//
//  MSHomeViewController.swift
//  SwiftWeibo
//
//  Created by 牟松 on 16/8/1.
//  Copyright © 2016年 DoNews. All rights reserved.
//

import UIKit

private let OriginalStateCell = "OriginalStateCell"


class MSHomeViewController: MSBaseViewController {

    // 数据源数组
    private lazy var statesViewModel = HomeStatesListViewModel()
    

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func setupUI() {
        
        super.setupUI()
        
        if !MSAFNManage.shared.userLogin {
            navBar.tintColor = #colorLiteral(red: 0.9960784314, green: 0.5490196078, blue: 0.1411764706, alpha: 1)
            navItem.leftBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(loginButtonDidClick))
            navItem.rightBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(registButtonDidClick))
      
            
        } else {
            navItem.leftBarButtonItem = UIBarButtonItem(MS_BarButtonItem: "", imageName: "navigationbar_friendattention", selectedImageName: "navigationbar_friendattention_highlighted", target: self, action: #selector(leftBarButtonDidClick))
            
            navItem.rightBarButtonItem = UIBarButtonItem(MS_BarButtonItem: "", imageName: "navigationbar_icon_radar", selectedImageName: "navigationbar_icon_radar_highlighted", target: self, action: #selector(rightBarButtonDidClick))
            
            let titleViewName = MSUserAccountInfoModel.shared.name
            let titleViewButton = MSHomeTitleViewButton(userName: titleViewName ?? "首页")
            titleViewButton.addTarget(self, action: #selector(titleViewButtonDidClick), for: .touchUpInside)
 
            navItem.titleView = titleViewButton
        }
    }
    
    
    // MARK: - 加载数据
    override func loadData() {
        
        /// ispull: 用于判断是上拉还是下拉
        statesViewModel.loadStates(isPullup: ispullUp) { (statesList, isSuccess, shouleRefresh) in
            
            if isSuccess && shouleRefresh {
                self.tableview?.reloadData()                
            }
            
            self.refreshControl?.endRefreshing()
        }
        
        ispullUp = false
        
   
    }
    
    // MARK: - 监控事件
    
    @objc private func leftBarButtonDidClick() {
        navigationController?.pushViewController(MSFriendsArrentionViewController(), animated: true)
        
    }
    @objc private func rightBarButtonDidClick() {
        print("雷达按钮点击")
    }
    
    @objc private func titleViewButtonDidClick(button: UIButton){
        button.isSelected = !button.isSelected
    }

}
// MARK: - 设置UITableView
extension MSHomeViewController {
    
    override func setupTableView() {
        super.setupTableView()
        
        
        
        tableview?.register(UINib(nibName: "MSStatesNormalCell", bundle: nil), forCellReuseIdentifier: OriginalStateCell)
        
        tableview?.rowHeight = UITableViewAutomaticDimension
        tableview?.estimatedRowHeight = 300
        
    }
    
    
}
// MARK: - tableview的代理方法
extension MSHomeViewController {
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        
        return statesViewModel.statesList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableview?.dequeueReusableCell(withIdentifier: OriginalStateCell, for: indexPath) as! MSStatessCell
        
        
        cell.contentLabel.text = statesViewModel.statesList[indexPath.row].statesModel.text
        return cell
        
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 300
    }
    
   
}
